<?php

namespace app\user\logic\user;

use app\circle\logic\Circle;
use app\circle\logic\Comment as CircleComment;
use app\course\logic\comment\Comment;
use app\topic\model\TopicComment;
use app\user\model\user\NotifyMessage as NotifyMessageModel;
use ArrayObject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 消息通知
 */
class NotifyMessage extends NotifyMessageModel
{

    /**
     * 用户未读信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-08
     *
     * @return NotifyMessage[]|array|\think\Collection [type]                      [description]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function unread()
    {
        return $this->scope('unread')->select();
    }

    /**
     * 用户未读信息统计
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-08
     *
     * @param int $type
     * @return int [type]                      [description]
     */
    public function unreadCount($type = 0)
    {
        return request()->mid ? $this->scope('unread', $type)->count() : 0;
    }

    // 互动消息
    public function getCommentNotify($status)
    {
        $notifyMod = [
            1 => app(Comment::class),
            2 => app(TopicComment::class),
            3 => app(Question::class),
            4 => app(Circle::class),
            5 => app(CircleComment::class),
        ];

        return $this->with(['send_user.avatar_url'])->commentList($status)->order('create_time', 'DESC')
            // ->buildSql();
            ->paginate($this->getListRows())->each(function ($i) use ($notifyMod) {
                $mod = $notifyMod[$i->notify_type];
                if ($i->notify_type == 1) {
                    $data = $mod->with(['course_title'])->findOrEmpty($i->notify_data_id);
                } else {
                    $data = $mod->findOrEmpty($i->notify_data_id);
                }

                $i->notify_data = $data->isEmpty() ? new ArrayObject() : $data;
            });
    }

    // 提问消息
    public function getQuestionNotify($status)
    {
        return $this->with(['send_user.avatar_url'])->questionReplyList($status)->order('create_time', 'DESC')
            // ->buildSql();
            ->paginate($this->getListRows())->each(function ($i) {
                if ($i->notify_data_id) {
                    $data           = app(Question::class)->findOrEmpty($i->notify_data_id);
                    $i->notify_data = $data->isEmpty() ? new ArrayObject() : $data;
                } else {
                    $i->notify_data = new ArrayObject();
                }
            });
    }

    public function setRead($id)
    {
        $data = $this->findOrEmpty($id);
        if (!$data->isEmpty()) {
            $data->is_read = 1;
            $data->save();
        }
    }

    public function setAllRead($notify_type)
    {
        $list = $this->scope('unread', $notify_type)->select();

        if (!$list->isEmpty()) {
            $list->update(['is_read' => 1]);
        }
    }
}
